<template>
  <el-dialog v-model="addOrEditVisible" title="通知"
             :before-close="closeEditDialog"
             width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules"
             label-width="160px" class="demo-ruleForm"
             :size="formSize" status-icon>
      <!--                <el-row>-->
      <!--                    <el-col :span="24">-->
      <!--                        <el-form-item label="" prop="id">-->
      <!--                            <el-input v-model="ruleForm.id"/>-->
      <!--                        </el-form-item>-->
      <!--                    </el-col>-->
      <!--                </el-row>-->
      <el-row>
        <el-col :span="24">
<!--          <el-form-item label="信息类型" prop="msgType" >-->
<!--            &lt;!&ndash;            <el-input v-model="ruleForm.msgType"/>&ndash;&gt;-->
<!--            <el-select v-model="ruleForm.msgType" clearable style="width:100%"-->
<!--                       placeholder="请选择信息类型">-->
<!--              <el-option :label="'系统消息'" :value=0></el-option>-->
<!--              <el-option :label="'订单消息'" :value=1></el-option>-->
<!--              <el-option :label="'核销消息'" :value=2></el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="信息内容" prop="msgContent">
            <el-input type="textarea" v-model="ruleForm.msgContent"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="是否推送" prop="delayFlag" >
            <!--            <el-input v-model="ruleForm.delayFlag"/>-->
            <el-select v-model="ruleForm.delayFlag" clearable style="width:100%"
                       placeholder="请选择是否推送">
              <el-option :label="'否'" :value=0></el-option>
              <el-option :label="'是'" :value=1></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="推送用户 ,逗号隔开" prop="msgUser">-->
      <!--            <el-input v-model="ruleForm.msgUser"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="状态 0-停用 1-启用 2-备用" prop="statusFlag">-->
      <!--            <el-input v-model="ruleForm.statusFlag"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="是否删除 0-否 1-是" prop="delFlag">-->
      <!--            <el-input v-model="ruleForm.delFlag"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="创建人" prop="createUser">-->
      <!--            <el-input v-model="ruleForm.createUser"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="创建时间" prop="createTime">-->
      <!--            <el-input v-model="ruleForm.createTime"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="修改时间" prop="updateTime">-->
      <!--            <el-input v-model="ruleForm.updateTime"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
      <!--      <el-row>-->
      <!--        <el-col :span="24">-->
      <!--          <el-form-item label="备注" prop="remark">-->
      <!--            <el-input v-model="ruleForm.remark"/>-->
      <!--          </el-form-item>-->
      <!--        </el-col>-->
      <!--      </el-row>-->
    </el-form>
    <template #footer>
         <span class="dialog-footer">
           <el-button @click="closeEditDialog">关闭</el-button>
           <el-button type="primary"
                      @click="submitForm(ruleFormRef)">提交</el-button>
         </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'
import {ElMessage} from "element-plus";
import router from "@/router";
import {RetailMsg} from '@/apis/model/msg/RetailMsgModel';
import {insertRetailMsg, updateRetailMsg} from '@/apis/http/msg/RetailMsgHttp';

interface Prop {
  addOrEditVisible: boolean,
  ruleForm: RetailMsg,
  addDataType: string,
  urlName: string
}

// 刷新list页面
const refushList = ref(false);

// 传递过来的参数
const props = defineProps<Prop>();

const emit = defineEmits(["closeEditDialog"]);

// 关闭按钮
const closeEditDialog = () => {
  emit("closeEditDialog", {
    addOrEditVisible: !props.addOrEditVisible,
    refushList: refushList.value
  })
}
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  id: [
    {required: true, message: '请输入', trigger: 'blur'}
  ],
  msgType: [
    {required: true, message: '请输入信息类型 0-系统消息 1-订单消息 2-核销消息', trigger: 'blur'}
  ],
  msgContent: [
    {required: true, message: '请输入信息内容', trigger: 'blur'}
  ],
  delayFlag: [
    {required: true, message: '请输入是否已推送 0-否  1-是', trigger: 'blur'}
  ],
  msgUser: [
    {required: true, message: '请输入推送用户 ,逗号隔开', trigger: 'blur'}
  ],
  statusFlag: [
    {required: true, message: '请输入状态 0-停用 1-启用 2-备用', trigger: 'blur'}
  ],
  delFlag: [
    {required: true, message: '请输入是否删除 0-否 1-是', trigger: 'blur'}
  ],
  createUser: [
    {required: true, message: '请输入创建人', trigger: 'blur'}
  ],
  createTime: [
    {required: true, message: '请输入创建时间', trigger: 'blur'}
  ],
  updateTime: [
    {required: true, message: '请输入修改时间', trigger: 'blur'}
  ],
  remark: [
    {required: true, message: '请输入备注', trigger: 'blur'}
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  if (props.urlName == 'sysMsgList') {
    props.ruleForm.msgType = 0;
  }

  await formEl.validate((valid, fields) => {
    if (valid) {
      // 新增
      if (props.addDataType === 'add') {
        insertRetailMsg(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '创建成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('创建失败！')
          }
        })
      } else if (props.addDataType === 'edit') {
        updateRetailMsg(props.ruleForm).then(res => {
          const code = res.code;
          const message: string = res.message;
          const data = res.data;
          if (code == 200) {
            ElMessage({
              message: '更新成功！',
              type: 'success',
            })
            refushList.value = true
            closeEditDialog()
          } else {
            ElMessage.error('更新失败！')
          }
        })
      }

    } else {
      ElMessage.error('请完善信息！')
    }
  })
}
</script>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
